package com.zqxx.examine.management.controller.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.zqxx.examine.management.service.ExamPaperService;
import com.zqxx.examine.management.service.ExamService;
import com.zqxx.examine.model.exam.AnswerSheet;
import com.zqxx.examine.model.exam.AnswerSheetItem;
import com.zqxx.examine.model.exam.ExamPaper;
import com.zqxx.examine.model.exam.Message;

@Controller
public class ExamAction
{
  @Autowired
  private ExamPaperService examPaperService;
  @Autowired
  private ExamService examService;
  
  @RequestMapping(value={"/api/exampaper/{id}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public ExamPaper getExamPaper(@PathVariable("id") int id)
  {
    ExamPaper paper = this.examPaperService.getExamPaperById(id);
    return paper;
  }
  
  @RequestMapping(value={"/api/answersheet"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message submitAnswerSheet(@RequestBody AnswerSheet answerSheet)
  {
    List<AnswerSheetItem> itemList = answerSheet.getAnswerSheetItems();
    

    int approved = 3;
    for (AnswerSheetItem item : itemList) {
      if ((item.getQuestionTypeId() != 1) && (item.getQuestionTypeId() != 2) && (item.getQuestionTypeId() != 3))
      {
        approved = 2;
        break;
      }
    }
    Gson gson = new Gson();
    this.examService.updateUserExamHist(answerSheet, gson.toJson(answerSheet), approved);
    
    return new Message();
  }
}
